<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title id="biaoti">replay</title>
  <link rel="stylesheet" href="css/xterm.css">
  <script src="js/xterm.js"></script>
  <style>
    #terminal{
      box-shadow: 2px 2px 2px #d2cbcb;
    }
    body{
      overflow: hidden;
    }
    .monitor{
      width: 1.2vw;
      top: 0.4vh;
      position: relative;
    }
  </style>
</head>

<!-- 发送两个回车进行端口9999连接 -->
<body style="background-color: #FFF;color: #000;padding: 0;margin: 0;">

<div>
  <input type="file" name="file" multiple="multiple" id="files" onchange="choosefile()" />
</div>

<div id="terminal"></div>
<div id="replay"></div>
<div id="sshMonitor" style="height: 3vh;width: 50vw;position: relative;top: 0;right: 0vw;font-size: 10px;user-select: none;"></div>
<script>
  <!-- 屏幕大小 -->
  window_width = window.screen.width;
  window_height = window.screen.height;
  console.info("列数："+Math.floor(window_width/11))
  let term = new Terminal({
    rows: Math.floor(window_height/29),
    cols: Math.floor(window_width/13),
    rendererType: 'dom', //,dom
    fontSize: 18,
    fontWeight: '250', //字体加粗
    convertEol: true,
    fontFamily: `'DejaVu Sans Mono','Fira Mono', monospace`,
    cursorStyle: 'underline', //光标样式 bar、underline、block
    useStyle:true,
    rightClickSelectsWord: true,
    scrollback: 9999999,
    cursorBlink: true, // 光标闪烁
    disableStdin: false, //是否应禁用输入。
    bellStyle: 'sound', //终端响铃
    theme: {
      foreground: '#000', //字体
      selection: '#AAAAAA49',//文字选中背景颜色
      background: '#FFF', //背景色
      cursor: 'black', //光标颜色
      cursorAccent: '#000', //光标强调色
    }
  });

  term.open(document.getElementById('terminal'));

  function timePrint(out,time){
    setTimeout(function (){
      term.write(out)
    },time * 500)
  }
  function timePrintln(out,time){
    setTimeout(function (){
      term.writeln(out)
    },time * 500)
  }

  var replay = ""

  function choosefile () {
    term.clear();
    var fileList = document.getElementById('files').files;
    var nameStr = '';
    for (var i = 0; i < fileList.length; i++) {
      nameStr += `${i === 0 ? '' : ', '}${fileList[i].name}`;
      var reader = new FileReader();
      reader.readAsText(fileList[i], "UTF-8");
      reader.onload = function (e) {
        var content = e.target.result;
        var lines = content.split('\r')
        testList = content.split('\r')
        for(var line = 0; line < lines.length; line++){

          //TODO 播放器
          // 录像时间原点
          origin = parseInt(lines[5].split("-")[1]).toFixed(2);

          // 发现命令结果
          if (lines[line].includes("JainaCmdResultTime:")){
            cmdTime = parseInt(lines[line].split(":")[1].split("JainaCmdResult:")[0]).toFixed(2) - origin;
            // 找结果中的结束标记
            for (var i = line;i < lines.length; i++) {
              if (lines[i].includes("JainaCmdResultEnd")) {
                //包含结束标志
                if (lines[i].includes("JainaCmdResult:")){
                  cmdTime = parseInt(lines[line].split(":")[1].split("JainaCmdResult:")[0]).toFixed(2) - origin;
                  result = lines[i].split("JainaCmdResult:")[1].replace(/JainaCmdResultEnd/g,"")
                  // TODO 输入命令时，键盘左右移动问题
                  if (result == "[D " || result == "[C "){
                    console.info("方向键给爷死!")
                  }else if (lines[i].includes("\b")){
                    timePrintln(result.replace("\b ","\b"),cmdTime);
                    // timePrintln(result.replace("\b ","\b").replace("[C","d"),cmdTime);
                  }else {
                    timePrint(result,cmdTime);
                  }
                  break;
                }else {
                  console.info(lines[i])
                  result = lines[i].replace(/JainaCmdResultEnd/g,"")
                  timePrint(result,cmdTime);
                  break;
                }
              }
              else if (lines[i].includes("JainaCmdResult:")){
                // 不包含结束标志 && 不包含命令标志 && 包含结果第一行标志 第一行去掉标记
                result = lines[i].split("JainaCmdResult:")[1].replace(/JainaCmdResult/g,"")
                cmdTime = parseInt(lines[i].split(":")[1].split("JainaCmdResult:")[0]).toFixed(2) - origin;
                timePrintln(result,cmdTime);
              }
              else if (!lines[i].includes("JainaCmd:")  && !lines[i].includes("JainaCmdResultTime")){
                // //啥都不包含
                result = lines[i]
                timePrintln(result,cmdTime);
              }
            }
          }else if (lines[line].includes("JainaCmd:")){
            cmdOut = lines[line].split("JainaCmd:")[1]
            cmdTime = parseInt(lines[line].split("JainaCmd:")[0].split(":")[1]).toFixed(2) - origin;
            // 如果命令结尾，最后一个字符占一行，让命令结果跑到下一行去
            // if (lines[line+1] == "\n" || lines[line+1] == "" || lines[line+1].includes("JainaCmdResultTime:")){
            if (lines[line+1] == "\n" || lines[line+1] === ""){
              console.info("命令:" + cmdOut)
              timePrintln(cmdOut,cmdTime);
            }else if (lines[line+1].includes("JainaCmdResult:")){
              timePrint(cmdOut,cmdTime);
              timePrint("\n",cmdTime);
            }else{
              console.info("命令:" + cmdOut)
              timePrint(cmdOut,cmdTime);
            }
          }
          for (var j = lines.length-1;0 < j < lines.length; j--) {
            if (lines[j].includes("JainaCmdResultTime") || lines[j].includes("JainaCmdTime")){
              var end = parseInt(lines[j].split(":")[1].split(" ")[0]).toFixed(2);
              replay = end - origin
              break;
            }
          }
        }
        // 显示replay时间长度
        document.getElementById("replay").innerText="录像时长：" + replay + "秒";
      }
    }
  }


  // 鼠标左键选中既复制
  var container = container ||  document;
  container.onmouseup = function(){
    copy = window.getSelection();
    document.execCommand('copy');
  }
</script>
</body>
</html>